<?php
$name=$_GET['name'];
$table='tttj.channel';

if($_GET['op']=='cache')
{
	$list_ios = array();
	$list_android = array();
	$query=exec_db("select channel,cha_name,pt from $table");
    while($value=mysql_fetch_assoc($query))
    {
//         $list[$value['channel']]=trim($value['cha_name']);
        if ($value['pt']==2){
        	$list_ios[$value['channel']] = trim($value['cha_name']);
        }
        if ($value['pt']==1){
        	$list_android[$value['channel']] = trim($value['cha_name']);
        }
    }
    /* $content='<?php ';
    $content .= "\$channel = ". var_export($list,true) . ";\r\n";
    $content .= ' ?>'; */
    $content_ios='<?php ';
    $content_ios .= "\$channel = ". var_export($list_ios,true) . ";\r\n";
    $content_ios .= ' ?>';
    $content_android='<?php ';
    $content_android .= "\$channel = ". var_export($list_android,true) . ";\r\n";
    $content_android .= ' ?>';
//     echo $content;exit;
    file_put_contents(S_ROOT . 'source/data/channel_ios' . $appid . '.php', $content_ios);
    file_put_contents(S_ROOT . 'source/data/channel_android' . $appid . '.php', $content_android);
    curl_get("http://127.0.0.1:8100/index.php?tp=crontab/channel_cache&appid={$appid}");

    cpmessage("��������ɹ�!","nadmin.php?ac={$ac}&appid={$appid}");
    exit;
}
if($_POST["op"]=="edit")
{
	$id=intval($_POST["id"]);
	$channel=trim($_POST["channel"]);
	$cha_name=$_POST["cha_name"];
	$pt = (int)$_POST['pt'];
	if($id)
	{
		$sql="update $table set channel='$channel',cha_name='$cha_name',pt=$pt where id=$id";
		exec_db($sql);
		cpmessage("�����޸ĳɹ�!","nadmin.php?ac={$ac}&appid={$appid}");
	}
	else
	{
		$ontime=time();
		$sql="insert into $table(channel,pt,cha_name,ontime,appid) values('$channel','$pt','$cha_name','$ontime',$appid)";
		exec_db($sql);
		cpmessage("������ӳɹ�!","nadmin.php?ac={$ac}&appid={$appid}");
	}
	exit;
}

if($_GET["op"]=="del")
{
	$id=intval($_GET["id"]);
	$sql="delete from $table where id=$id";
	exec_db($sql);
	cpmessage("����ɾ���ɹ�!","nadmin.php?ac={$ac}&appid={$appid}");
}

$tpl->assign('nav','��������');

if($_GET["op"]=="add")
{
	$id=0;
	$tpl->assign('id',$id);
	$tpl->assign('op','add');
}
else if($_GET["op"]=="edit")
{
	$id=intval($_GET["id"]);
	$sql="select * from $table where id=$id";
	$row=getonerecord($sql);
	$tpl->assign('row',$row);
	$tpl->assign('op','edit');
}
else
{
    if ($name) {
        $wheresql.=" and (channel like '%$name%' or cha_name like '%$name%')";
        $mpurl.="&name=$name";
        $tpl->assign('name', $name);
    }
    $channel = $_GET['channel'];
    if ($channel)
    {
    	$wheresql .= " and channel='$channel'";
    }	
	$sql="select * from $table $wheresql order by id desc";
// 	echo $sql;exit;
	$query = exec_db($sql);
	while($value = mysqli_fetch_assoc($query))
	{
		$value['ontime']=date('Y-m-d H:i:s',$value['ontime']);
		$list[]=$value;
	}
	$tpl->assign("list",$list);
}
$tpl->display("admin/{$ac}.html");


?>